package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.Gwdc005Export;
import cn.e4j.modular.crm.entity.GwddT;
import cn.e4j.modular.crm.service.impl.Gwdc005ServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 合同协议维护
 * Created by miaoyl on 2019-2-1.
 */
@Controller
@RequestMapping("/crm/gwdc005")
public class Gwdc005Controller extends BaseController {

    @Autowired
    private Gwdc005ServiceImpl gwdc005Service;
    @Autowired
    public SysAuthorityService sysAuthorityService;

    /**
     * 跳转合同协议维护管理页面
     */
    @RequiresPermissions("crm:gwdc005:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwdc005.html";
    }

    /**
     * 查询合同协议维护信息列表
     */
    @RequestMapping("/getGwddlist")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData getGwddlist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdc005Service.getGwddlist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询合同协议信息列表
     */
    @RequestMapping("/getGwddData")
    @ResponseBody
    public ResponseData getGwddData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdc005Service.getGwddData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwddT> gwddTList = inDto.getReqList(GwddT.class);
        gwdc005Service.addData(gwddTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwddT> gwddTList = JSONObject.parseArray(jsonObject.getString("list1"), GwddT.class);
        gwdc005Service.editData(gwddTList,jsonObject.getString("GWDD001"));
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwdc005Service.delete(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwddT> gwddTList = inDto.getReqList(GwddT.class);
        gwdc005Service.delDataBatch(gwddTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改单据状态
     */
    @RequestMapping("/updateData")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData updateData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwddT> gwddTList = inDto.getReqList(GwddT.class);
        gwdc005Service.updateData(gwddTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取新增数据
     */
    @RequestMapping("/getAddData")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = gwdc005Service.getAddData(inDto);
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 查询客户分类信息列表
     */
    @RequestMapping("/getGwdeList")
    @ResponseBody
    public ResponseData getGwdeList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdc005Service.getGwdelist(inDto);
        return ResponseData.success(result);
    }

    @RequestMapping("/getGwddocno")
    @ResponseBody
    public ResponseData getGwddocno(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdc005Service.getGwddocno(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping(value = "/exportsData", method = RequestMethod.GET)
    @RequiresPermissions("crm:gwdc005:init")
    public void exportsData(ModelMap map, HttpServletRequest request, HttpServletResponse response, @DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser user = ShiroKit.getUser();
        inDto.put("LANG", LangUtils.getLang(user.getLANG()));
        inDto.put("GWDBENT", user.getDBC());
        //客户权限
        inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
        //部门权限
        inDto.put("ORGS", sysAuthorityService.getOrgsAuthority(inDto));
        //List<Map<String, Object>> list = gwdc005Service.getGwddlist(null, inDto);
        List<Map<String, Object>> list = InvokeUtil.invokeBeanMethod("gwddTMapper,listGwddPage", inDto);
        JSONArray json_arr = new JSONArray();
        for (Map<String, Object> map1 : list) {
            JSONObject json_obj = new JSONObject();
            for (Map.Entry<String, Object> entry : map1.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                json_obj.put(key, value);
            }
            json_arr.add(json_obj);
        }
        List<Gwdc005Export> gwdc005Exports = new ArrayList<>();
        gwdc005Exports = JSONObject.parseArray(json_arr.toString(), Gwdc005Export.class);
        ExportParams params = new ExportParams(null, "合同协议数据", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "合同协议数据");
        map.put(NormalExcelConstants.DATA_LIST, gwdc005Exports);
        map.put(NormalExcelConstants.CLASS, Gwdc005Export.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
}
